/**
 * 功能:TK_REGIONS 控制器类
 * 开发人员:yaren
 * 创建时间:2012/4/21 23:50:11
 */
package com.towker.frame.controller.basicData;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.model.basicData.Regions;
import com.towker.frame.model.sell.SellLine;
import com.towker.frame.service.basicData.RegionsService;

//TK_REGIONS
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/basicData")
public class RegionsController
{
	private RegionsService service;

	//业务对象注入
	public void setRegionsService(RegionsService service)
	{
		this.service = service;
	}

	//添加
	@RequestMapping("/addRegions{viewName}")
	public ModelAndView addRegions(HttpServletResponse response, HttpServletRequest request, Regions po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		MyRequest req = new MyRequest(request);
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			String region_type = req.getString("region_type");
			if ("0".equals(region_type)) {
				result.setViewName("/manage/basicData/addRegions.jsp");
			}else if ("1".equals(region_type)) {
				result.setViewName("/manage/purchase/addRegions.jsp");
			}
			
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.add(po);
				writer.write("添加地区成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加地区失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delRegions")
	public void delRegions(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updRegions{viewName}")
	public ModelAndView updRegions(HttpServletRequest request, HttpServletResponse response, Regions po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			result.addObject("po", po);
			if (po.getRegion_type()==0) {
				result.setViewName("/manage/basicData/updRegions.jsp");
			}else if (po.getRegion_type()==1) {
				result.setViewName("/manage/purchase/updRegions.jsp");
			}
			
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.update(po);
				writer.write("修改地区成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改地区失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getRegions")
	public ModelAndView getRegions(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		String region_type = req.getString("region_type");
		Map map = req.getParameterValueMap(false, true);
		map.put("region_type", region_type);
		PageModel<Regions> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		if ("0".equals(region_type)) {
			result.setViewName("/manage/basicData/getRegions.jsp");
		}else  if ("1".equals(region_type)) {
			result.setViewName("/manage/purchase/getRegions.jsp");
		}
			
		
		return result;
	}

	//明细
	@RequestMapping("/getRegionsById")
	public ModelAndView getRegionsById(HttpServletRequest request, HttpServletResponse response)
	{
		Regions po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		if (po.getRegion_type()==0) {
			result.setViewName("/manage/basicData/getRegionsById.jsp");
		}else if (po.getRegion_type()==1) {
			result.setViewName("/manage/purchase/getRegionsById.jsp");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/checkExistsRegionCode")
	public String checkExistsRegionCode(HttpServletRequest request, HttpServletResponse response, Regions po){
		boolean result = service.checkExistsCode(po.getRegion_code(), po.getId());
		if (result) {
			return "{\"result\":1}";
		} else {
			return "{\"result\":0}";
		}
	}
}
